package com.ysx.wemedia.controller;
import com.ysx.common.constants.SystemConstants;
import com.ysx.common.pojo.Result;
import com.ysx.common.utils.RequestHeaderInfoUtils;
import com.ysx.exception.ToutiaoException;
import com.ysx.wemedia.pojo.WmMaterial;
import com.ysx.wemedia.service.WmMaterialService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
import com.ysx.core.controller.AbstractCoreController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.HttpRequestHandlerServlet;

import java.time.LocalDateTime;

/**
* <p>
* 自媒体图文素材信息表 控制器</p>
* @author ljh
* @since 2021-09-08
*/
@Api(value="自媒体图文素材信息表",tags = "WmMaterialController")
@RestController
@RequestMapping("/wmMaterial")
public class WmMaterialController extends AbstractCoreController<WmMaterial> {

    private WmMaterialService wmMaterialService;

    //注入
    @Autowired
    public WmMaterialController(WmMaterialService wmMaterialService) {
        super(wmMaterialService);
        this.wmMaterialService=wmMaterialService;
    }
    //自媒体人添加素材
    @PostMapping
    @Override
    public Result insert(@RequestBody WmMaterial wxMaterial) throws ToutiaoException {
        //从请求头中获取用户id
        String userId = RequestHeaderInfoUtils.getHeaderUserId();
        if (userId == null) {
            throw new ToutiaoException("没有用户id");
        }
        wxMaterial.setUserId(Integer.parseInt(userId));//TODO 暂时硬编码
        wxMaterial.setType(0);
        wxMaterial.setIsCollection(5);
        wxMaterial.setCreatedTime(LocalDateTime.now());
        //保存到数据库中
        wmMaterialService.save(wxMaterial);
        return Result.ok(wxMaterial);
    }

}

